package com.dpad.crmclientapp.android.modules.newapp.presenter


import com.dpad.crmclientapp.android.modules.newapp.ui.fragment.UserCenterFragment
import com.dpad.telematicsclientapp.mvp.kit.Kits
import com.dpad.telematicsclientapp.mvp.mvp.XPresent
import com.dpad.telematicsclientapp.netlibrary.MainApplicaton
import com.dpad.telematicsclientapp.netlibrary.http.NetInstance
import com.dpad.telematicsclientapp.netlibrary.newapp.entity.UserCenterVo
import com.dpad.telematicsclientapp.netlibrary.newapp.entity.YckzZipEntity
import com.dpad.telematicsclientapp.util.utils.ParameterUtils
import com.socks.library.KLog
import io.reactivex.Observer
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.Disposable
import io.reactivex.schedulers.Schedulers
import java.util.*

/**
 * ================================================
 * 作    者：booob
 * 版    本：1.0
 * 创建日期：2018-05-18-0018 17:30
 * 描    述：
 * 修订历史：
 * ================================================
 */
class UserCenterPresenter : XPresent<UserCenterFragment>() {


    fun getUserCenterData(map: SortedMap<String, Any>, isRefresh: Boolean, needLoadImage: Boolean) {
        KLog.e("加载数据","个人中心")
        if (isRefresh) {
            v.loading("正在加载...")
        }
        NetInstance.getEventsService().getUserCenter(ParameterUtils.getHeaser(map), ParameterUtils.getJsonBody(map))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(object : Observer<UserCenterVo> {
                    override fun onComplete() {
                        v.loadingComplete()
                    }

                    override fun onSubscribe(d: Disposable?) {
                        addDisposable(d)
                    }

                    override fun onNext(value: UserCenterVo) {
                        value!!.let {
                            if (value.code == "00000") {
                                if (needLoadImage) {
                                    getCarPicUrl()
                                }
                                v.showData(value)
                            }
                        }
                    }

                    override fun onError(e: Throwable?) {
                        v.loadingComplete()
                        if (!isRefresh) {
                            v.loadMoreFail()
                        }
                    }

                })
    }


    //获取图片链接地址
    fun getCarPicUrl() {
//        populevehicle 我的爱车车图
//        uservehicle 个人中心车图
//        ecovehicle 环保驾驶车图
//        indexvehicleinfo 首页车况报告车图
//        vehicleinfoblue 车况报告车图蓝底
//        vehicleinfored 车况报告车图红底
//        vehicleinfoyellow 车况报告车图黄底

        v.loading("正在加载...")
        val sortedMap: SortedMap<String?, Any?> = TreeMap()
        sortedMap["saleSubmodelId"] = MainApplicaton.getSaleSubmodelId()
        sortedMap["downloadType"] = "1" //0为zip 1为车型图片 2为远控图片
        NetInstance.getEventsService().picDownload(ParameterUtils.getHeaser(sortedMap), ParameterUtils.getJsonBody(sortedMap))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(object : Observer<YckzZipEntity> {
                    override fun onSubscribe(d: Disposable) {
                        addDisposable(d)
                    }

                    override fun onNext(bean: YckzZipEntity) {
                        if (bean.code == "00000") {
                            MainApplicaton.urlMap.clear()
                            if (!bean.result.isNullOrEmpty()) {

                                for (item in bean.result) {
                                    if (!Kits.Empty.check(item.mark)) {
                                        when (item.mark) {
                                            "populevehicle" -> {
                                                MainApplicaton.urlMap["populevehicle"] = item.downloadPath
                                            }
                                            "uservehicle" -> {
                                                MainApplicaton.urlMap["uservehicle"] = item.downloadPath
                                            }
                                            "ecovehicle" -> {
                                                MainApplicaton.urlMap["ecovehicle"] = item.downloadPath
                                            }
                                            "indexvehicleinfo" -> {
                                                MainApplicaton.urlMap["indexvehicleinfo"] = item.downloadPath
                                            }
                                            "vehicleinfoblue" -> {
                                                MainApplicaton.urlMap["vehicleinfoblue"] = item.downloadPath
                                            }
                                            "vehicleinfored" -> {
                                                MainApplicaton.urlMap["vehicleinfored"] = item.downloadPath
                                            }
                                            "vehicleinfoyellow" -> {
                                                MainApplicaton.urlMap["vehicleinfoyellow"] = item.downloadPath
                                            }
                                        }
                                    }
                                }
                            }
                            v.loadPIc()
                        }
                    }

                    override fun onError(e: Throwable) {
                        v.loadingComplete()
                        v.dealHttpError(e)
                    }

                    override fun onComplete() {
                        v.loadingComplete()
                    }
                })
    }
}
